/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.applyperiod.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nmf.dao.gen.BizApplyPeriodMapper;
import com.nmf.dao.gen.bean.BaseCategory;
import com.nmf.dao.gen.bean.BizApplyPeriod;
import com.nmf.dao.gen.bean.BizApplyPeriodExample;
import com.nmf.site.biz.category.service.BaseCategoryService;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.IdGenerater;
import com.nmf.site.modules.sys.utils.UserUtils;

/**
 * 申请周期Service
 * @author bm
 * @version 2019-02-13
 */
@Service
@Transactional(readOnly = true)
public class BizApplyPeriodService{

	@Autowired
	BizApplyPeriodMapper bizApplyPeriodMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizApplyPeriod get(String no) {
		return bizApplyPeriodMapper.selectByPrimaryKey(no);
	}
	
	public List<BizApplyPeriod> findList(BizApplyPeriod bizApplyPeriod) {
		BizApplyPeriodExample bizApplyPeriodExample = new BizApplyPeriodExample();
		BizApplyPeriodExample.Criteria criteria = bizApplyPeriodExample.createCriteria();
		
		return bizApplyPeriodMapper.selectByExample(bizApplyPeriodExample);
	}
	
	public Page<BizApplyPeriod> findPage(Page<BizApplyPeriod> page, BizApplyPeriod bizApplyPeriod) {
		BizApplyPeriodExample bizApplyPeriodExample = new BizApplyPeriodExample();
		BizApplyPeriodExample.Criteria criteria = bizApplyPeriodExample.createCriteria();
		
		if(bizApplyPeriod.getStartDate()!=null) {
			Date date = new Date();//可用的周期
			criteria.andStartDateLessThan(date);
			criteria.andEndDateGreaterThan(date);
		}
		bizApplyPeriodExample.setPage(page);
		
		List<BizApplyPeriod> dlist = bizApplyPeriodMapper.selectByExample(bizApplyPeriodExample);
		page.setList(dlist);
		page.setCount(bizApplyPeriodMapper.countByExample(bizApplyPeriodExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(BizApplyPeriod bizApplyPeriod) {
		setCategory(bizApplyPeriod);
		if(StringUtils.isNotEmpty(bizApplyPeriod.getId())&&get(bizApplyPeriod.getId())!=null) {
			bizApplyPeriodMapper.updateByPrimaryKeySelective(bizApplyPeriod);
		}else {
			Date date = new Date();
			bizApplyPeriod.setCreateDate(date);
			bizApplyPeriod.setId(idGenerater.genClsNo("bizApplyPeriod"));
			bizApplyPeriod.setUpdateDate(date);
			bizApplyPeriodMapper.insertSelective(bizApplyPeriod);
		}
	}
	@Autowired
	BaseCategoryService baseCategoryService;
	private void setCategory(BizApplyPeriod bizApplyPeriod) {
		if(StringUtils.isNotBlank(bizApplyPeriod.getSubCategoryId())) {
			BaseCategory baseCategory = baseCategoryService.get(bizApplyPeriod.getSubCategoryId());
			bizApplyPeriod.setSubCategoryName(baseCategory.getCategoryName());
			
			bizApplyPeriod.setCategoryId(baseCategory.getParentId());
		}
		if(StringUtils.isNotBlank(bizApplyPeriod.getCategoryId())) {
			BaseCategory parentBaseCategory = baseCategoryService.get(bizApplyPeriod.getCategoryId());
			bizApplyPeriod.setCategoryName(parentBaseCategory.getCategoryName());
		}
	}
	@Transactional(readOnly = false)
	public void delete(BizApplyPeriod bizApplyPeriod) {
		bizApplyPeriodMapper.deleteByPrimaryKey(bizApplyPeriod.getId());
	}
	
}